package com.prp.gwt.client.widgets.user.php;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.user.php.PHPListener;
import com.prp.gwt.client.vo.Message;

public class MsgItem extends VerticalPanel {

	HTML name;
	
	public MsgItem(final Message msg) {
		addStyleName("msgitem");
		HorizontalPanel innerPanel = new HorizontalPanel();
		Image img = new Image(msg.getFromUser().getPortrait());
		img.addStyleName("msgitem-image");
		innerPanel.add(img);
		VerticalPanel vPanel = new VerticalPanel();
		HorizontalPanel titlePanel = new HorizontalPanel();
		VerticalPanel opPanel = new VerticalPanel();
		titlePanel.addStyleName("msgitem-title");
		opPanel.addStyleName("msgitem-sidebar");
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			name = new HTML("<a>" + msg.getFromUser().getUsername()
					+ "</a>");
			name.addStyleName("msgitem-name");
			hPanel.add(name);
			Label date = new Label(msg.getDate());
			date.addStyleName("msgitem-time");
			hPanel.add(date);
			titlePanel.add(hPanel);
		}
		if (MsgBoardPanel.getInstance().getCurrentUser().equals(
				MsgBoardPanel.getInstance().getUser())) {
			HTML delete = new HTML("<a>删除</a>");
			delete.addStyleName("msgitem-link");
			opPanel.add(delete);
			//titlePanel.setCellHorizontalAlignment(delete, ALIGN_RIGHT);
			delete.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (Window.confirm("确实要删除留言?"))
						PHPListener.getInstance().deleteMessage(msg.getId());
				}
			});
		}
		if (!msg.getFromUser().equals(
				MsgBoardPanel.getInstance().getCurrentUser())) {
			HTML reply = new HTML("<a>回复</a>");
			reply.addStyleName("msgitem-link");
			opPanel.add(reply);
			//titlePanel.setCellHorizontalAlignment(reply, ALIGN_RIGHT);
			reply.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					MsgBoardPanel.getInstance().setToUser(msg.getFromUser());
				}
			});
			name.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(PHPHistoryHandler.PREFIX_USERHOME+msg.getFromUser().getId());
				}
			});
		}
		else{
			name.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(PHPHistoryHandler.INIT_VALUE);
				}
			});
		}
		vPanel.add(titlePanel);
		Label content = new Label(msg.getContent());
		content.addStyleName("msgitem-content");
		vPanel.add(content);
		innerPanel.add(vPanel);
		innerPanel.add(opPanel);
		add(innerPanel);
	}

}
